﻿/*
 *
 *au:zhuyehong
 */
(function (win) {
  var path = "";
  var url = "/C/getDocumentList.asp?r=" + top.VALIDATION_STRING;
  var files = [];
  var folders = [];

  var preHtml = "<div class='ShoppingCart' ><table width='100%' border='0' align='center' cellpadding='0' cellspacing='0' class='fileContents' >";
  var afterHtml = "</table></div>";

  /*Attr******************************/
  var s_position = "f-browser";
  //文件浏览窗口显示位置
  var canChangeDir = false;
  var defaultDir = "/mnt/log";
  ///OAM/
  /*******************************/

  /*callback begin*****************/
  var changeDirCallback = "";
  var selectFileCallback = "";
  /*callback end*****************/

  function changeShowPosition(position) {
    $("#" + s_position).hide().empty();
    s_position = position;
  }

  function changeDefaultDir(dir) {
    defaultDir = dir;
  }

  function changeRight(changedir) {
    canChangeDir = changedir;
  }

  function Open(dir) {
    if (dir == null) {
      //dir = "/";
      dir = defaultDir;

      if (dir.length == 0)
        dir = "/";
    } else {
      dir = dir.replace("\n", "");
      dir = dir.replace(" ", "");
    }

    $.ajax({
      url: url,
      type: "post",
      contentType: "text/html",
      async: false,
      cache: false,
      timeout: 30010,
      data: {
        info: dir
      },
      success: function (data) {
        GetFilesAndDir(data);
        var html = CreateFoldersList();
        html = html + CreateFilesList();
        //listDiv
        html = preHtml + html + afterHtml;

        $("#" + s_position).empty();
        $("#" + s_position).addClass("nodisplay");
        $("#" + s_position).append(html);

        eventBind();

        $("#" + s_position).show();
      },
      error: function (XMLHttpRequest, textStatus, errorThrow) {
        alert(top.T.AlertInfo.ConnectFailInfo);
      }
    });

  }

  function GetFilesAndDir(data) {
    var pos = data.lastIndexOf("<h2><b>");
    if (pos != -1) {
      data = data.substring(0, pos - 1);
    }

    var list = data.split('\n');
    list.pop();
    //丢弃最后一个无效的文件

    folders = [];
    files = [];

    if (canChangeDir == true)
      folders.push("/.. ");

    for (var i = 0; i < list.length; i++) {
      list[i] = list[i].replace(/\?/g, "");
      list[i] = list[i].replace(/(^\s*)|(\s*$)/g, "");
      if (list[i].indexOf("/") != -1) {
        folders.push(list[i].replace(/\//g, ""));
      } else {
        files.push(list[i].replace(/\*|@|\||=/, ""));
      }
    }

    files = files.sort(function (a, b) {
      var len = (a.length > b.length ? b.length : a.length);
      var i = 0;

      for (; i < len; i++) {
        if (a.charAt(i) > b.charAt(i)) {
          return 1;
        }
        if (a.charAt(i) < b.charAt(i)) {
          return -1;
        }
      }

      return 1;
    });

    return {
      files: files,
      folders: folders
    };
  }

  function CreateFoldersList() {
    var foldersHtml = "";

    if (folders.length > 0) {
      var temp = folders[folders.length - 1];
      if (temp.length >= 1) {
        var indexF = folders[folders.length - 1].indexOf("\n");

        if (indexF != -1 && (indexF > (temp.length / 2))) {
          folders[folders.length - 1] = folders[folders.length - 1].substring(0, indexF);
        }

        foldersHtml = "<tr>";
        var j = 1;
        for (var j = 1; j < folders.length + 1; j++) {

          foldersHtml = foldersHtml + "<td width='10'>" + j + "</td>" + "<td class='fileBrowser-folder' height='20' style='cursor:pointer' ><a name='d' style='cursor:pointer'>" + folders[j - 1] + "</a></td></tr>";
        }

        foldersHtml = foldersHtml + "</tr>";
      }
    }

    return foldersHtml;
  }

  function CreateFilesList() {
    var objsHtml = "";
    if (files.length > 0) {
      var temp = files[files.length - 1];
      if (temp.length >= 1) {
        var index = files[files.length - 1].indexOf("\n");

        if (index != -1 && (index > (temp.length / 2))) {
          files[files.length - 1] = files[files.length - 1].substring(0, index);
        }

        objsHtml = "<tr>";
        j = 1;
        for (var j = 1; j < files.length + 1; j++) {

          objsHtml = objsHtml + "<td width='20'>" + j + "</td>" + "<td class='fileBrowser-file' height='20' style='cursor:pointer' ><a name='f' class='file' style='word-wrap:break-word;cursor:pointer'>" + files[j - 1] + "</a></td></tr>";

        }

        objsHtml = objsHtml + "</tr>";

        //$("#folderList").append(objsHtml);
      }
    }

    return objsHtml;
  }

  function eventBind() {
    $(".fileBrowser-file").unbind();
    $(".fileBrowser-file").bind("mouseover", FileBrowser.document_over);
    $(".fileBrowser-file").bind("mouseout", FileBrowser.document_out);
    $(".fileBrowser-file").bind("click", FileBrowser.changeFile);

    if (canChangeDir == true) {
      $(".fileBrowser-folder").unbind();
      $(".fileBrowser-folder").bind("mouseover", FileBrowser.document_over);
      $(".fileBrowser-folder").bind("mouseout", FileBrowser.document_out);
      $(".fileBrowser-folder").bind("click", FileBrowser.changeDir);
    }
  }

  function getCurPath() {
    var url = "/C/getCurPath.asp?r=" + top.VALIDATION_STRING;

    $.ajax({
      url: url,
      type: "post",
      contentType: "text/html",
      async: false,
      cache: false,
      timeout: 30010,
      data: {
        cmd: "null"
      },
      success: function (data) {
        path = data.replace(/\/\//, "/");
      },
      error: function (XMLHttpRequest, textStatus, errorThrow) {
        alert(top.T.AlertInfo.ConnectFailInfo);
      }
    });

    return path;
  }

  function document_out() {
    $(this).children().css("color", "rgb(36,96,168)");
  }

  function document_over() {
    $(this).children().css("color", "red");
  }

  function changeDir() {
    var dir = $(this).text();

    Open(dir);

    var curPath = getCurPath();
    if (typeof changeDirCallback == 'function') {
      changeDirCallback(curPath);
    }

    return curPath;
  }

  function getStandardDir() {
    var curPath = getCurPath();

    if (curPath !== "/") {
      if (curPath[curPath.length - 1] !== '/') {
        curPath = curPath + "/";
      }
    }

    return curPath;
  }

  function changeFile() {
    var filename = $(this).text();
    filename = filename.replace(/\s/g, "");
    filename = filename.replace(/\//g, "");

    var curPath = getStandardDir();
    var absPath = curPath + filename;

    if (typeof selectFileCallback == 'function') {
      selectFileCallback(absPath);
    }

    return absPath;
  }

  function init() {
    Open("/");
    Open();
    selectFileCallback = (ShoppingCart.AddFile);
  }

  win.FileBrowser = {
    init: init,
    document_out: document_out,
    document_over: document_over,
    changeDir: changeDir,
    changeFile: changeFile,
    GetFilesAndDir: GetFilesAndDir,
    Open: Open,
    getStandardDir: getStandardDir
  };
})(window);

(function (win) {
  var files = [];
  //cart
  var preHtml = "<div id='ShoppingCart-vector' class='ShoppingCart' ><table width='100%' border='0' align='center' cellpadding='0' cellspacing='0' class='fileContents' >";
  var afterHtml = "</table></div>";
  var capacity = 5;

  /********************/
  var s_position = "shoppingCart";
  /********************/

  function AddFile(file) {
    var i = 0, j = 0, isExist = false, tmpVector = [], dir = "", tmpstr = "";

    if (files.length >= capacity) {
      //parent.modalTips_cover();
      parent.Modal.open(top.T.Main.Notice, top.T.Main.FileTooMany, "error", function () {
      });
      return;
    }

    if (typeof file === 'object') {
      dir = FileBrowser.getStandardDir();

      for (i = 0; i < file.length; i++) {
        isExist = false;
        tmpstr = dir + file[i];

        for (j = 0; j < files.length; j++) {
          if (files[j] === tmpstr) {
            isExist = true;
            break;
          }
        }

        if (!isExist)
          tmpVector.push(tmpstr);
      }

      for (i = 0; i < tmpVector.length; i++) {
        if (tmpVector[i].length > 0)
          files.push(tmpVector[i]);
      }
    } else {
      for (; i < files.length; i++) {
        if (files[i] == file) {
          return;
        }
      }

      files.push(file);
    }

    reFresh();
  }

  function reFresh() {
    var html = "";

    for (var i = 0; i < files.length; i++) {
      html = html + "<tr><td width='20'>" + (i + 1) + "</td><td class='shoppingCart-file' height='20' style='cursor:pointer' title='单击删除' ><a name='f' class='file selected' style='cursor:pointer'>" + files[i] + "</a></td></tr>";
    }

    html = preHtml + html + afterHtml;

    $("#" + s_position).empty();
    $("#" + s_position).append(html);
    $("#" + s_position).show();

    $(".shoppingCart-file").bind("click", (ShoppingCart.delFile));

    var scrollHeightx = $($("#ShoppingCart-vector").children()[0]).height();
    if ($("#ShoppingCart-vector").attr("scrollTop")) {
      $("#ShoppingCart-vector").attr("scrollTop", scrollHeightx);
    } else {
      $("#ShoppingCart-vector")[0].scrollTop = scrollHeightx;
    }
  }

  function delFile() {
    var filename = $(this).text();

    for (var i = 0; i < files.length; i++) {
      if (filename == files[i]) {
        files.splice(i, 1);
        break;
      }
    }

    reFresh();
  }

  function GetFiles() {
    return files.join("\017");
  }

  function deleteAll() {
    files = [];
    reFresh();
  }


  win.ShoppingCart = {
    init: init,
    AddFile: AddFile,
    delFile: delFile,
    GetFiles: GetFiles,
    deleteAll: deleteAll
  };

  function init() {
    if (parent.USER.hasModule(parent.Branch.Modules.DisableLogCapacityLimit)) {
      capacity = 65535;
    }

    $("#" + s_position).addClass("ShoppingCartfileWarp");
    $("#" + s_position).show();
  }
})(window);
